package com.slh.touchandroid.utils;

import java.lang.reflect.Method;

import android.content.Context;
import android.telephony.TelephonyManager;

public class SIMUtils {
	// =========================================================================================================
	public static SimInfo getSIM1(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		SimInfo sim = new SimInfo();
		sim.serial = telephonyManager.getSimSerialNumber();
		sim.number = telephonyManager.getLine1Number();
		sim.deviceId = telephonyManager.getDeviceId();
		return sim;
	}

	public static SimInfo getSIM2(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		SimInfo sim = new SimInfo();
		Class<?> telephonyManagerClass = TelephonyManager.class;

		try {
			Method getSimSerialNumberGemini = telephonyManagerClass
					.getDeclaredMethod("getSimSerialNumberGemini", Integer.TYPE);
			getSimSerialNumberGemini.setAccessible(true);
			sim.serial = (String) getSimSerialNumberGemini.invoke(
					telephonyManager, 1);
			Method getLine1NumberGemini = telephonyManagerClass
					.getDeclaredMethod("getLine1NumberGemini", Integer.TYPE);
			getLine1NumberGemini.setAccessible(true);
			sim.number = (String) getLine1NumberGemini.invoke(telephonyManager,
					1);

			Method getDeviceIdGemini = telephonyManagerClass.getDeclaredMethod(
					"getDeviceIdGemini", Integer.TYPE);
			getDeviceIdGemini.setAccessible(true);
			sim.deviceId = (String) getDeviceIdGemini.invoke(telephonyManager,
					1);
		} catch (Throwable e) {
			Logging.log(e);
		}
		try {
			Method getSimSerialNumberDs = telephonyManagerClass
					.getDeclaredMethod("getSimSerialNumberDs", Integer.TYPE);
			getSimSerialNumberDs.setAccessible(true);
			sim.serial = (String) getSimSerialNumberDs.invoke(telephonyManager,
					1);
			Method getLine1NumberDs = telephonyManagerClass.getDeclaredMethod(
					"getLine1NumberDs", Integer.TYPE);
			getLine1NumberDs.setAccessible(true);
			sim.number = (String) getLine1NumberDs.invoke(telephonyManager, 1);

			Method getDeviceIdDs = telephonyManagerClass.getDeclaredMethod(
					"getDeviceIdDs", Integer.TYPE);
			getDeviceIdDs.setAccessible(true);
			sim.deviceId = (String) getDeviceIdDs.invoke(telephonyManager, 1);
		} catch (Throwable e) {
			Logging.log(e);
		}

		try {
			Method getTmBySlot = telephonyManagerClass.getDeclaredMethod(
					"getTmBySlot", Integer.TYPE);
			getTmBySlot.setAccessible(true);
			TelephonyManager tm = (TelephonyManager) getTmBySlot.invoke(
					telephonyManager, 1);
			sim.serial = tm.getSimSerialNumber();
			sim.number = tm.getLine1Number();
			sim.deviceId = tm.getDeviceId();
		} catch (Throwable e) {
			Logging.log(e);
		}
		return sim;
	}

	public static boolean isDualSIM(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		SimInfo sim = new SimInfo();
		Class<?> telephonyManagerClass = TelephonyManager.class;

		try {
			Method getSimSerialNumberGemini = telephonyManagerClass
					.getDeclaredMethod("getSimSerialNumberGemini", Integer.TYPE);
			getSimSerialNumberGemini.setAccessible(true);
			sim.serial = (String) getSimSerialNumberGemini.invoke(
					telephonyManager, 1);
			Method getLine1NumberGemini = telephonyManagerClass
					.getDeclaredMethod("getLine1NumberGemini", Integer.TYPE);
			getLine1NumberGemini.setAccessible(true);
			sim.number = (String) getLine1NumberGemini.invoke(telephonyManager,
					1);

			Method getDeviceIdGemini = telephonyManagerClass.getDeclaredMethod(
					"getDeviceIdGemini", Integer.TYPE);
			getDeviceIdGemini.setAccessible(true);
			sim.deviceId = (String) getDeviceIdGemini.invoke(telephonyManager,
					1);
			return true;
		} catch (Throwable e) {
			Logging.log(e);
		}
		try {
			Method getSimSerialNumberDs = telephonyManagerClass
					.getDeclaredMethod("getSimSerialNumberDs", Integer.TYPE);
			getSimSerialNumberDs.setAccessible(true);
			sim.serial = (String) getSimSerialNumberDs.invoke(telephonyManager,
					1);
			Method getLine1NumberDs = telephonyManagerClass.getDeclaredMethod(
					"getLine1NumberDs", Integer.TYPE);
			getLine1NumberDs.setAccessible(true);
			sim.number = (String) getLine1NumberDs.invoke(telephonyManager, 1);

			Method getDeviceIdDs = telephonyManagerClass.getDeclaredMethod(
					"getDeviceIdDs", Integer.TYPE);
			getDeviceIdDs.setAccessible(true);
			sim.deviceId = (String) getDeviceIdDs.invoke(telephonyManager, 1);
			return true;
		} catch (Throwable e) {
			Logging.log(e);
		}

		try {
			Method getTmBySlot = telephonyManagerClass.getDeclaredMethod(
					"getTmBySlot", Integer.TYPE);
			getTmBySlot.setAccessible(true);
			TelephonyManager tm = (TelephonyManager) getTmBySlot.invoke(
					telephonyManager, 1);
			sim.serial = tm.getSimSerialNumber();
			sim.number = tm.getLine1Number();
			sim.deviceId = tm.getDeviceId();
			return true;
		} catch (Throwable e) {
			Logging.log(e);
		}
		return false;
	}

	public static class SimInfo {
		public String serial;
		public String number;
		public String label;
		public String deviceId;

	}

}
